package com.init.bootframe.business.service.impl;

import com.init.bootframe.business.dao.ISwiperDao;
import com.init.bootframe.business.service.ISwiperService;
import com.init.bootframe.business.vo.SwiperVO;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SwiperServiceImpl implements ISwiperService {

    private Logger logger = LoggerFactory.getLogger(SwiperServiceImpl.class); //日志

    @Autowired
    private ISwiperDao swiperDao; //轮播图dao


    /**
     * 分页查询
     * @param swiperVO
     * @return
     */
    @Override
    public JSONObject findSwiperList(SwiperVO swiperVO) {
        JSONObject jsonObject = new JSONObject();
        // 查询集合数据总数
        int total = swiperDao.findSwiperCount(swiperVO);
        // 查询集合数据
        List<SwiperVO> swiperList = new ArrayList<>();
        if (total > 0) {
            swiperList = swiperDao.findSwiperList(swiperVO);
        }
        jsonObject.put("rows", swiperList);
        jsonObject.put("total", total);
        return jsonObject;
    }

    /**
     * list查询
     * @param swiperVO
     * @return
     */
    @Override
    public List<SwiperVO> findSwiperLists(SwiperVO swiperVO) {
        return swiperDao.findSwiperList(swiperVO);
    }

    /**
     * 详情查询
     * @param swiperVO
     * @return
     */
    @Override
    public SwiperVO findSwiper(SwiperVO swiperVO) {
        return swiperDao.findSwiper(swiperVO);
    }

    /**
     * 更新轮播图信息
     * @param swiperVO
     * @return
     */
    @Override
    public Map<String, Object> updateSwiper(SwiperVO swiperVO) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            swiperDao.updateSwiper(swiperVO);
            resultMap.put("result", Boolean.TRUE);
            resultMap.put("msg", "修改成功！");
            logger.debug("修改轮播图信息成功！");
        } catch (Exception e) {
            resultMap.put("result", Boolean.FALSE);
            resultMap.put("msg", "修改失败！");
            logger.debug("修改轮播图信息失败！" + e.getMessage());
            throw new RuntimeException(e.getMessage());
        }
        return resultMap;
    }

}
